package com.climb.moive.filter;


import com.climb.moive.entity.Admin;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter(filterName ="AdminLoginFilter",urlPatterns = {"/adminEvaluation/*","/adminMember/*","/adminMovie/*"})
public class AdminLoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        System.out.println("进入管理员登录过滤器");
        /*获取session中的member用来判断*/
        HttpSession session=request.getSession();
        Admin admin=(Admin) session.getAttribute("admin");
        HttpServletResponse response=(HttpServletResponse) servletResponse;
        if(admin==null){
            /*放行*/
            request.getRequestDispatcher(request.getContextPath() + "/view/admin/AdminLogin.jsp").forward(request,response);
            return;
        }
        /*正常放行*/
        filterChain.doFilter(servletRequest,servletResponse);
    }


    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
